package com.minSYSU.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {
	
	static final String dbName = "minsysu";
	static final String name = "";
	static final String recordsTable = "records";

	public DatabaseHelper(Context context) {
		super(context, dbName, null, 1);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		db.execSQL("CREATE TABLE " + recordsTable +
				 " (_id INTEGER PRIMARY KEY AUTOINCREMENT," +
				 " userID text not null ," + 
				 " Content text not null," +
				 " Time DATETIME not null," +
				 " Location text not null)");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub

		db.execSQL("DROP TABLE IF EXISTS " + recordsTable);
	}
	
	
	public Cursor getAllRecords() {
		SQLiteDatabase db = this.getWritableDatabase();
		 
		Cursor cur = db.rawQuery("SELECT * FROM " + recordsTable, null);
		return cur;
		 
	}
	 
	public void AddRecords(Records Con) {			 
		SQLiteDatabase db = this.getWritableDatabase();
		 
		ContentValues cv = new ContentValues();
		
		cv.put("userID", Con.get_name());
		cv.put("Content", Con.get_content());
		cv.put("Time", Con.get_sendTime());
		cv.put("Location", Con.get_location());
		
		db.insert(recordsTable, "userID", cv);
	}
	
	public Cursor getRecordsByTime(String beginTime, String endTime) {
		SQLiteDatabase db = this.getReadableDatabase();
		
		Cursor cur = db.rawQuery("select * from records Where Time between ? and ?", 
				new String[]{beginTime, endTime});
		return cur;		
	}
	 
	public int getCount() {
		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cur = db.rawQuery("Select * from " + recordsTable, null);
		int x = cur.getCount();
		return x;
	}
}
